<template>
	<view class="content"> 
		<view class="logo"><image src="/static/login/logo.png" mode=""></image></view>
		<view class="uni-form-item uni-column">
			<input v-model="phone" type="tel" class="uni-input" name="" placeholder="请输入用户名/手机号" />
		</view>
		<view class="uni-form-item uni-column">
			<input v-model="password" type="password" class="uni-input" name="" placeholder="请输入登陆密码" />
		</view>
		<button type="primary" @tap="loginUser">登陆</button>
		<view class="links"><view @tap="gotoForgetPassword">忘记密码？</view><view>|</view><view class="link-highlight" @tap="gotoRegistration">注册账号</view></view>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				phone:"",
				password:""
			}
		},
		onLoad() {
			
		},
		methods: {
			...mapMutations(['login']),
			gotoRegistration: function () {
				uni.navigateTo({
					url: `/pages/login/registration`
				})
			},
			gotoForgetPassword: function () {
				uni.navigateTo({
					url: `/pages/login/forget-password`
				})
			},
			loginUser:function () {
				var me = this;
				uni.showLoading({
					title:''
				})
				if (me.phone.length == 0) {
					me.$api.msg('请输入手机号');
					return;
				}
				if (me.phone.length != 11) {
					me.$api.msg('请输入合法手机号');
					return;
				}
				if (me.password.length ==0) {
					me.$api.msg('请输入登录密码');
					return;
				}
				
				uni.request({
					url: me.serverUrl + '/core/api/v1/hy/passport/login',
					method: "POST",
					data: {
						'userUid': me.phone,
						'password': me.password
					},
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						
						if (res.data.code == 200) {
							me.requestUserInfo(res.data.data);
							uni.setStorage({//缓存用户登陆状态
							    key: 'token',  
							    data: res.data.data  
							}) 
						} else {
							
							uni.hideLoading();
							me.$api.msg(res.data.msg);
						}
					},
					complete: (error) => {
						// uni.hideLoading();
						console.log(error)
					}
				})
			},
			requestUserInfo: function (token) {
				var me = this;
				uni.request({
					url: me.serverUrl + '/core/api/v1/hy/info/cur',
					method: "GET",
					header: {
						'content-type': 'application/json',
						'Authorization': 'Bearer '+token
					},
					success: (res) => {
						
						if (res.data.code == 200) {
							uni.navigateBack({
								
							})
							me.login(res.data.data)
						}
					},
					complete: (error) => {
						
						// uni.hideLoading();
						console.log(error)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #12B3F9;
	.content{
		padding: 100upx;
	}
	.logo{
	    text-align: center;
		image{
		    height: 200upx;
		    width: 200upx;
		    margin: 0 0 60upx;
		}
	}
	.uni-form-item{
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;
		.uni-input{
			font-size: 30upx;
			padding: 7px 0;
		}
	}
	button[type="primary"]{
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}
	.links{
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;
		view{
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}
		.link-highlight{
			color: $color-primary
		}
	}
</style>
